
##########################################################################
# Figure 2:	Efficiency in the allocation of political attention, 2001-2014
##########################################################################

# Programme:  cfsp-analysis02-frequency.r
# Author:     Frank Haege, Department of Politics and Administration, University of Limerick
# Contact:		frank.haege@ul.ie

# Description
#############
# This script creates Figure 2 of the article. 


# Remove all objects
rm(list = ls(all = TRUE))

# Load libraries
library(data.table)
library(ggplot2)
library(ggpubr)
library(MASS)

# Set working directory
setwd("C:\\Users\\Frank\\Dropbox\\European Union\\CFSP attention\\EUP submission")

# Set options
options(stringsAsFactors=F)

# # Read data
load("Data management\\cfsp-management02-semesterData.RData")

# Drop general meeting variables
df = df[!(df$id=="C00" | df$id=="D00" | df$id=="C51"), ]
# These categories include meetings that could not be unambiguously allocated to any particular working party

# Create interaction variable
df$treat[df$lisbonsem==0 & df$comp=="CFSP"] = 1
df$treat[df$lisbonsem==0 & df$comp=="Ext. Rel."] = 2
df$treat[df$lisbonsem==1 & df$comp=="CFSP"] = 3
df$treat[df$lisbonsem==1 & df$comp=="Ext. Rel."] = 4
df$treat = factor(df$treat, labels = c("CFSP pre-Lisbon", "Ext. Rel. pre-Lisbon", "CFSP post-Lisbon", "Ext. Rel. post-Lisbon"))
table(df$treat, df$comp)

# Changes in mean duration
mean(df$dur[df$treat=="CFSP pre-Lisbon"])
mean(df$dur[df$treat=="Ext. Rel. pre-Lisbon"])
mean(df$dur[df$treat=="CFSP post-Lisbon"])
mean(df$dur[df$treat=="Ext. Rel. post-Lisbon"])

# Negative binomial regression
negbin = glm.nb(data=df, formula = dur ~ time + treat + time:treat)
summary(negbin)
negbin = glm.nb(data=df, formula = dur ~ time*lisbonsem*comp)
summary(negbin)

# Predict fitted values and standard errors
pv = data.frame(predict(negbin, se.fit=T, type="response"))
pv = cbind(df, pv)
pv$ci.low = pv$fit-1.96*pv$se.fit
pv$ci.high = pv$fit+1.96*pv$se.fit

# Plot time vs. duration and overlay with prediction lines
p = ggplot() +
  geom_point(data=pv, aes(x=time, y=dur, shape=factor(comp), fill=factor(comp)),
             size=5, alpha = 0.4, color="gray10", position = position_jitter(w = .2, h = 0)) +
  geom_ribbon(data=pv[pv$treat=="Ext. Rel. pre-Lisbon", ],aes(x=time, ymin=ci.low,ymax=ci.high), fill="gray60", alpha=0.9) +
  geom_ribbon(data=pv[pv$treat=="Ext. Rel. post-Lisbon", ],aes(x=time, ymin=ci.low,ymax=ci.high), fill="gray60", alpha=0.9) +  
  geom_line(data=pv[pv$treat=="Ext. Rel. pre-Lisbon", ], aes(x=time, y=fit), size=1) +
  geom_line(data=pv[pv$treat=="Ext. Rel. post-Lisbon", ], aes(x=time, y=fit), size=1) +
  geom_ribbon(data=pv[pv$treat=="CFSP pre-Lisbon", ],aes(x=time, ymin=ci.low,ymax=ci.high), fill="gray30", alpha=0.9) +
  geom_ribbon(data=pv[pv$treat=="CFSP post-Lisbon", ],aes(x=time, ymin=ci.low,ymax=ci.high), fill="gray30", alpha=0.9) +
  geom_line(data=pv[pv$treat=="CFSP pre-Lisbon", ], aes(x=time, y=fit), size=1) +
  geom_line(data=pv[pv$treat=="CFSP post-Lisbon", ], aes(x=time, y=fit), size=1) +
  ylab("Number of meetings") +
  xlab("Year") +
  geom_vline(xintercept=18.5, linetype="dashed") +
  scale_shape_manual(values=c(21, 23)) +
  scale_fill_manual(values=c("gray1", "gray60")) +
  scale_x_continuous(breaks=c(3, 11, 19, 27), labels=c("2002", "2006", "2010", "2014")) +
  theme_bw(base_size=20) +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.title=element_blank(),
        legend.key.size = unit(1, "cm"))
p

# Export plot
ggexport(p, filename="Data analysis\\Figures\\cfsp-analysis02-frequency.png", width=800, height=500)
